package org.algorithm;

import java.util.ArrayList;
import java.util.List;

public class Snow {

    //符号位
    private static final int SIGNED_BITS = 1;

    //发号类型位
    private static final int GEN_TYPE_BITS = 3;

    private static final int GEN_TYPE_RIGHT_BITS = 64 - SIGNED_BITS - GEN_TYPE_BITS;

    //工作节点位
    private static final int WORKER_ID_BITS = 3;

    private static final int WORKER_ID_RIGHT_BITS = 64 - SIGNED_BITS - GEN_TYPE_BITS - WORKER_ID_BITS;

    //序列号位
    private static final int SEQUENCE_BITS = 13;

    private static final long TIMESTAMP_INIT_SEQ= Long.parseLong("0000000111111111111111111111111111111111111111111110000000000000", 2);


    private static final long MAX_WORKER_ID = ~(-1L << WORKER_ID_BITS);

    private static final long MAX_GEN_TYPE = ~(-1L << GEN_TYPE_BITS);

    private static final long SEQUENCE_MAX = ~(-1L << SEQUENCE_BITS);

    private final long workerId;

    private final long genType;
    private long sequence = 0L;
    private long lastTimestamp = -1L;

    public Snow(long workerId, long genType) {


        if (workerId > MAX_WORKER_ID || workerId < 0) {
            throw new IllegalArgumentException("Worker ID must be between 0 and " + MAX_WORKER_ID);
        }

        if (genType > MAX_GEN_TYPE || genType < 0) {
            throw new IllegalArgumentException("GenType ID must be between 0 and " + MAX_GEN_TYPE);
        }
        this.workerId = workerId;
        this.genType = genType;
    }

    public synchronized long generateId() {
        long timestamp = System.currentTimeMillis();

        if (timestamp < lastTimestamp) {
            throw new RuntimeException("Clock moved backwards. Refusing to generate ID for " + (lastTimestamp - timestamp) + " milliseconds.");
        }

        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & SEQUENCE_MAX;
            if (sequence == 0) {
                // Sequence overflow, wait until next millisecond
                timestamp = waitNextMillis(lastTimestamp);
            }
        } else {
            // Reset sequence for the new millisecond
            sequence = 0L;
        }

        lastTimestamp = timestamp;

//        long x= (genType << GEN_TYPE_RIGHT_BITS);
//
//        long y= (workerId << WORKER_ID_RIGHT_BITS);
//
//        long v=(timestamp << SEQUENCE_BITS);
//
//        long z= (v & TIMESTAMP_INIT_SEQ);
//
//        System.out.println(Long.toBinaryString(x));
//
//        System.out.println(Long.toBinaryString(y));
//
//        System.out.println(Long.toBinaryString(timestamp));
//
//        System.out.println(Long.toBinaryString(v));
//
//        System.out.println(Long.toBinaryString(z));


//        long f =(genType << GEN_TYPE_RIGHT_BITS) |
//                (workerId << WORKER_ID_RIGHT_BITS) |
//                ((timestamp << SEQUENCE_BITS) & TIMESTAMP_INIT_SEQ) |
//                sequence;
//
//        System.out.println(Long.toBinaryString(f));

        return (genType << GEN_TYPE_RIGHT_BITS) |
                (workerId << WORKER_ID_RIGHT_BITS) |
                ((timestamp << SEQUENCE_BITS) & TIMESTAMP_INIT_SEQ) |
                sequence;
    }

    private long waitNextMillis(long lastTimestamp) {
        long timestamp = System.currentTimeMillis();
        while (timestamp <= lastTimestamp) {
            timestamp = System.currentTimeMillis();
        }
        return timestamp;
    }

    public static void main(String[] args) {
        Snow idGenerator = new Snow(0, 1);
        Long mill = System.currentTimeMillis();
        List<String> ids = new ArrayList<>(50000);
        for (int i = 0; i < 50000; i++) {
            long id = idGenerator.generateId();
//            System.out.println("Generated ID: " + Long.toHexString(id));
            String idStr = Long.toHexString(id);
            ids.add(idStr);
        }
        System.out.println(System.currentTimeMillis() - mill);
        for (String id:ids) {
            System.out.println(id);
        }
    }


}
